package bejar;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;


public class MyCellRenderder extends JLabel implements ListCellRenderer{
	
	
	final static ImageIcon longIcon = new ImageIcon("src/images/folder.png");
    final static ImageIcon shortIcon = new ImageIcon("src/images/folder.png");
    
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		String s = value.toString();
		setText(s);
		setIcon((s.length()> 20) ? longIcon : shortIcon);
		if(isSelected){
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
			
		}else{
			setEnabled(list.isEnabled());
			setBackground(list.getBackground()); 
			setForeground(list.getForeground());

		}
		
		Font font = new Font("Serif", Font.BOLD, 18);
		setEnabled(list.isEnabled());
		setFont(font);
		setOpaque(true);
		return this;
	}
	
	
	public static void main(String [] args){
		JFrame frame = new JFrame();
		frame.setBackground(Color.WHITE);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(new Dimension(200, 300));

		String[] data = {"Library", "Momo's pic", "Metz pic", "Jos pic"};
		final JList dataList = new JList(data);
		dataList.setCellRenderer(new MyCellRenderder());
		JScrollPane scroll = new JScrollPane(dataList);
		
		MouseListener mouselistener = new MouseAdapter(){
			public void mouseClicked (MouseEvent e){
				if(e.getClickCount()== 1){
					int index = dataList.locationToIndex(e.getPoint());
					System.out.println("Double clicked on item "+ index);
				}
				
			}
		};
		
		dataList.addMouseListener(mouselistener);
		
		frame.getContentPane().add(scroll);
		frame.setVisible(true);
	}

}
